<?php
/**
 * 菜单基类
 * User: Administrator
 * Date: 2018\9\10 0010
 * Time: 17:18
 */

namespace app\Lib\Model\AdminAuth\AdminMenu;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\AdminAuth\AdminMenu\AdminMenuBaseHandle;
use app\Lib\InterfaceIn\AdminAuth\MenuAuthHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\AdminAuth\MenuAuth;
use app\Lib\Model\DbModel\AdminMenu;
use app\validate\AdminMenuValidate;

abstract class AdminMenuBase implements AdminMenuBaseHandle
{
    /** @var UserHandle $opUser */
    protected $opUser;
    /** @var MenuAuth $parentClass */
    protected $parentClass;
    protected $name;
    protected $project;
    protected $c;
    protected $a;
    protected $args;
    protected $sortNo;
    protected $showMenu;
    protected $menuClass;
    protected $status = 1;
    protected $menuDb;
    protected $validate;
    protected $mark;
    protected $cacheName = 'menuAuthUpdateTime';
    public function __construct(UserHandle $opUserObj)
    {
        $this->opUser = $opUserObj;
    }

    /**
     * 菜单名称
     * @param $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * 项目名称
     * @param $project
     * @return $this
     */
    public function setProject($project)
    {
        $this->project = $project;
        return $this;
    }

    /**
     * 设置控制器
     * @param $c
     * @return $this
     */
    public function setC($c)
    {
        $this->c = $c;
        return $this;
    }

    /**
     * 设置方法
     * @param $a
     * @return $this
     */
    public function setA($a)
    {
        $this->a = $a;
        return $this;
    }

    /**
     * 设置参数
     * @param $args
     * @return $this
     */
    public function setArgs($args)
    {
        $this->args = $args;
        return $this;
    }

    /**
     * 设置父级类
     * @param MenuAuthHandle $parentClass
     * @return $this
     */
    public function setParentClass(MenuAuthHandle $parentClass)
    {
        $this->parentClass = $parentClass;
        return $this;
    }


    /**
     * 设置排序
     * @param $sortNo
     * @return $this
     */
    public function setSortNo($sortNo)
    {
        $this->sortNo = $sortNo;
        return $this;
    }

    /**
     * 设置菜单显示状态
     * @param $showMenu
     * @return $this
     */
    public function setShowMenu($showMenu)
    {
        $this->showMenu = $showMenu;
        return $this;
    }

    /**
     * 菜单图标
     * @param $menuClass
     * @return $this
     */
    public function setMenuClass($menuClass)
    {
        $this->menuClass = $menuClass;
        return $this;
    }

    /**
     * 初级菜单标识
     * @param $mark
     * @return $this
     */
    public function setMark($mark)
    {
       $this->mark = $mark;
       return $this;
    }

    /**
     * 数据整合
     * @return array
     * @throws BlException
     */
    protected function handleArray(){
        $db     = $this->getMenuDb();
        $level  = $this->getLevel();
        if($level == 1 && empty($this->mark)){
            throw new BlException('初级菜单必须提供菜单标识');
        }
        if($level!=1){
            $this->mark = '';
        }
        $data = [
            $db->_name=>$this->name,
            $db->_c=>$this->c,
            $db->_a=>$this->a,
            $db->_project=>$this->project,
            $db->_parent_id=>$this->parentClass->getMenuId(),
            $db->_status=>$this->status,
            $db->_args=>$this->args,
            $db->_menu_class=>$this->menuClass,
            $db->_show_menu=>$this->showMenu,
            $db->_sort_no=>$this->sortNo,
            $db->_level=>$level,
            $db->_mark=>$this->mark,
        ];
        return $data;
    }

    protected function getMenuDb(){
        if(empty($this->menuDb)){
            $this->menuDb = new AdminMenu();
        }
        return $this->menuDb;
    }

    /**
     * 获取等级
     * @return int
     */
    protected function getLevel(){
        if($this->parentClass->getMenuId() == 0){
            return 1;
        }else{
            return ($this->parentClass->getLevel())+1;
        }
    }

    /**
     * 获取验证
     * @return AdminMenuValidate
     */
    protected function getValidate(){
        if(empty($this->validate)){
            $this->validate = new AdminMenuValidate();
        }
        return $this->validate;
    }

    public function saveTime(){
        /*菜单更新时间*/
        cache($this->cacheName,time(),604800);
    }
}